/**
 * 
 */
package com.nd.android.payment.util;

import java.util.UUID;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.common.StarAppConfig;
import com.common.StarAppConst;
import com.common.allCommon.StarappApplicationVariable;
import com.common.utils.EncryptTool;
import com.common.widget.CommonTipDlg;
import com.common.widget.CommonTipDlg.OnCommonDlgClickListener;
import com.nd.android.payment.R;
import com.nd.commplatform.NdCommplatform;
import com.product.android.business.ApplicationVariable;
import com.product.android.commonInterface.BaseCommonStruct;
import com.product.android.commonInterface.CIConst;
import com.product.android.commonInterface.CommonInterfaceManager;

/**
 * 
 * <br>Created 2014-11-14 上午11:04:21
 * @version  
 * @author   zhangqr		
 *
 * @see 	 
 */

public class PaymentUtils {
    private static final String TAG = PaymentUtils.class.getSimpleName();
    
    /**
     * 91SDK 检查是否已经登录 
     * @return
     */
    public static boolean is91SDKLogin(Context ctx){
//        return true;
        if (!NdCommplatform.getInstance().isLogined()) {
            return false; 
        } else {
            return true;
        }           
    }
    
    /**
     * 注销登录确认框
     * 
     * <br>Created 2014-11-21 上午10:24:26
     * @author       zhangqr
     */
    public static void sureToLogout(final Context context) {
        CommonTipDlg mLogOutCommonTipDlg = new CommonTipDlg(context,
                context.getString(R.string.pay_logout_tip),
                context.getString(R.string.pay_logout_btn_left),
                context.getString(R.string.pay_logout_btn_right),
                R.style.common_dialog_style);
        mLogOutCommonTipDlg.setLayoutResID(R.layout.common_tip_dlg_pink);
        mLogOutCommonTipDlg.setOnListener(new OnCommonDlgClickListener() {
            public void onRightBtnClicked() {
                logout(context);
                
            }
            
            public void onLeftBtnClicked() {
                // TODO Auto-generated method stub
                
            }
        });
        mLogOutCommonTipDlg.show();
    }
    
    /**
     * 退出登录
     * 
     * <br>Created 2014-11-21 下午5:04:25
     * @param context
     * @author       张清荣
     */
    public static void logout(Context context) {
        BaseCommonStruct param = new BaseCommonStruct();
        param.obj1 = context;
        int ret = CommonInterfaceManager.INSTANCE.CommonInterfaceID(
                CIConst.MAIN_MODELID, CIConst.MAIN_DO_LOGOUT_90011,
                param);
        if (ret != CIConst.COM_RET_OK) {
            Log.e(TAG, "logout : " + ret);
        }
        return;
    }
    
    /**
     * 获取UUID
     * 
     * <br>Created 2014-11-14 下午3:06:19
     * @return
     * @author       zhangqr
     */
    public static String getUUID() {
        UUID guid = UUID.randomUUID();
        String serial = guid.toString();
        serial = serial.replace("-", "".trim());
        return serial;
    }
    
    /**
     * 返回token
     * 
     * <br>Created 2014-11-20 下午6:18:29
     * @param context   上下文
     * @return
     * @author       zhangqr
     */
    public static String getToken(Context context) {
        SharedPreferences sp = context.getSharedPreferences(StarAppConst.PAY.PAY_SHAREDPREFERENCES, Activity.MODE_PRIVATE);
        if(sp != null) {
            String token = "";
            try {
                token = EncryptTool.desDecrypt(
                            sp.getString(ApplicationVariable.INSTANCE.getUapUid()+"", ""),
                            StarAppConst.PAY.PAY_ENCRYPT_KEY);
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
                token = "";
            }
            return token;
        }
        return "";
    }

    /**
     * 返回serial
     *
     * <br>Created 2014-11-20 下午6:18:29
     * @param context   上下文
     * @return
     * @author       zhangqr
     */
    public static String getSerial(Context context) {
        return getUUID();
    }

    /**
     * 返回productId
     *
     * <br>Created 2014-11-20 下午6:18:29
     * @param context   上下文
     * @return
     * @author       zhangqr
     */
    public static String getProductId(Context context) {
        SharedPreferences sp = context.getSharedPreferences(StarAppConst.PAY.PAY_SHAREDPREFERENCES, Activity.MODE_PRIVATE);
        if(sp != null) {
            return sp.getString(StarAppConfig.CONFIG_PAY_DIAMOND_PRODUCT_ID, "");
        }
        return "";
    }

    /**
     * 返回productName
     *
     * <br>Created 2014-11-20 下午6:18:29
     * @param context   上下文
     * @return
     * @author       zhangqr
     */
    public static String getProductName(Context context) {
        SharedPreferences sp = context.getSharedPreferences(StarAppConst.PAY.PAY_SHAREDPREFERENCES, Activity.MODE_PRIVATE);
        if(sp != null) {
            return sp.getString(StarAppConfig.CONFIG_PAY_DIAMOND_PRODUCT_NAME, "");
        }
        return "";
    }

    /**
     * 返回productPrice
     *
     * <br>Created 2014-11-20 下午6:18:29
     * @param context   上下文
     * @return
     * @author       zhangqr
     */
    public static double getProductPrice(Context context) {
        SharedPreferences sp = context.getSharedPreferences(StarAppConst.PAY.PAY_SHAREDPREFERENCES, Activity.MODE_PRIVATE);
        double price = StarAppConst.PAY.DEFAULT_PRODUCT_PRICE;
        if(sp != null) {
            String strPrice = sp.getString(StarAppConfig.CONFIG_PAY_DIAMOND_PRODUCT_PRICE, "");
            try {
                price = Double.parseDouble(strPrice);
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
                price = StarAppConst.PAY.DEFAULT_PRODUCT_PRICE;
            }
        }
        return price;
    }
    
    /**
     * 获得用户名
     * 
     * <br>Created 2014-11-20 下午6:19:55
     * @return
     * @author       zhangqr
     */
    public static String getUserName() {
        return StarappApplicationVariable.INSTANCE.getCurrentUserInfo().getUserName();
    }

}
